home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Libraries / WASTE 1.1 / WEUtilities.c < prev   
Encoding:
C/C++ Source or Header  |  1994-10-31  |  1.9 KB  |  107 lines  |  [TEXT/MPCC]

  1. /*
  2.  *    Utilities.c
  3.  *
  4.  *    WASTE PROJECT
  5.  *    General purpose utility
  6.  *
  7.  *    Copyright (c) 1993-1994 Marco Piovanelli
  8.  *    All Rights Reserved
  9.  *
  10.  */
  11.  
  12. #ifndef __TYPES__
  13. #include <Types.h>
  14. #endif __TYPES__
  15.  
  16. #ifndef __MEMORY__
  17. #include <Memory.h>
  18. #endif __MEMORY__
  19.  
  20. #include "WASTEIntf.h"
  21. pascal Boolean _WEBlockCmp(register Ptr block1, register Ptr block2, register long blockSize)
  22. {
  23.     for ( ; blockSize > 0 ; blockSize-- )
  24.         if ( *block1++ != *block2++ )
  25.             return false;
  26.     
  27.     return true;
  28. }
  29.  
  30. pascal void _WEBlockClr(register Ptr block, register long blockSize)
  31. {
  32.     for ( ; blockSize > 0; blockSize-- )
  33.         *block++ = 0;
  34. }
  35.  
  36. pascal void _WEForgetHandle(Handle *h)
  37. {
  38.     Handle theHandle = *h;
  39.     
  40.     if (theHandle != NULL)
  41.     {
  42.         *h = NULL;
  43.         DisposeHandle(theHandle);
  44.     }
  45. }
  46.  
  47. pascal Boolean _WESetHandleLock(Handle h, Boolean lock)
  48. {
  49.     Boolean oldLock = (HGetState(h) & (1 << 7)) != 0;
  50.     
  51.     if (lock != oldLock)
  52.         if (lock)
  53.             HLock(h);
  54.         else
  55.             HUnlock(h);
  56.             
  57.     return oldLock;
  58. }
  59.  
  60. pascal void _WEReorder(long *a, long *b)
  61. {
  62.     if (*a > *b)
  63.     {
  64.         register long temp = *a;
  65.         *a = *b;
  66.         *b = temp;
  67.     }
  68. }
  69.  
  70. pascal OSErr _WEAllocate(Size blockSize, short allocFlags, Handle *h)
  71. {
  72.     //{ Allocate a new relocatable block. }
  73.     //{ AllocFlags may specify whether the block should be cleared and whether }
  74.     //{ temporary memory should be used. }
  75.  
  76.     Handle theHandle;
  77.     OSErr retval;
  78.     
  79.     theHandle = nil;
  80.  
  81.     //{ if kAllocTemp is specified, try tapping temporary memory }
  82.     if ((allocFlags & kAllocTemp) != 0) 
  83.     {
  84.         theHandle = TempNewHandle(blockSize, &retval);
  85.     }
  86.     //{ if kAllocTemp isn't specified, or TempNewHandle failed, try with current heap }
  87.     if (theHandle == nil) 
  88.     {
  89.         theHandle = NewHandle(blockSize);
  90.         retval = MemError();
  91.     }
  92.     
  93.     //{ if kAllocClear is specified, zero the block }
  94.     if ((allocFlags & kAllocClear) != 0) 
  95.     {
  96.         if (theHandle != nil) 
  97.         {
  98.             _WEBlockClr(*theHandle, blockSize);
  99.         }
  100.     }
  101.  
  102.     //{ return handle through VAR parameter }
  103.     *h = theHandle;
  104.     
  105.     return retval;
  106. }
  107.